<?php

class Obj_DBTable_Articles extends Zend_Db_Table_Abstract
{
    protected $_name = 'articles';
    protected $_primary = 'ID';
    
    public function getNoPagingArticles($activeOnly) {
    	$select = $this->select()->from($this)->order('Title');
    	if ($activeOnly === true) $select->where('Active = 1');
    	return $this->fetchAll($select)->toArray();
    }
    
    public function getAllArticles($page, $active_only = false) {
    	$select = $this->select()->from($this)->order("date desc");
    	if($active_only) $select = $select->where("active = 1");
    	$arResult = Zend_Paginator::factory($this->fetchAll($select));
    	$arResult->setItemCountPerPage(BACKEND_ITEMS_PER_PAGE);
    	$arResult->setCurrentPageNumber($page);
    	return $arResult;
    }
    
    public function getSitemapArticles() {
    	$select = $this->select()->from($this, array('alias', 'date'))->order("date desc");
    	$select = $select->where("active = 1");
    	return $this->fetchAll($select)->toArray();
    }
    
    public function getOneArticle($id, $activeOnly = flase) {
    	$select = $this->select()->from($this);
    	if (trim(intval($id)) == trim($id)) {
    		$select->where('ID = ?', (int) $id); 
    	}
    	else {
    		$select->where('alias = ?', $id);
    	}
    	if ($activeOnly === true) $select->where('active = 1');
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    } 
    
    public function delete($where)
    {
        return $this->_db->delete($this->_name, $where);
    }
        
    public function checkAlias($alias, $id) {
    	$select = $this->select()->from($this, array('cnt' => new Zend_Db_Expr('COUNT(ID)')));
    	$select->where('alias = ?', trim($alias));
    	$select->where('ID <> ?', (int) $id); 
    	$result = $this->fetchRow($select)->toArray();
    	return $result['cnt'] > 0;
    }
}